/**
* 版权声明：厦门睿商网络科技有限公司 版权所有 违者必究
* 日    期：2020-03-25
*/
package com.rzico.basics.controller.member;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.rzico.annotation.Log;
import com.rzico.base.BaseController;
import com.rzico.base.CommResult;
import com.rzico.basics.entity.Member;
import com.rzico.basics.service.MemberService;
import com.rzico.core.service.SysSequenceService;
import com.rzico.entity.PageResult;
import com.rzico.entity.Pageable;
import com.rzico.basics.entity.Coupon;
import com.rzico.basics.entity.CouponCode;
import com.rzico.basics.model.CouponCodeVo;
import com.rzico.basics.service.CouponCodeService;
import com.rzico.basics.service.CouponService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 我的卡券控制层
 *
 * @author Rzico Boot
 * @version 1.0
 * @date 2020-03-25
 */
@Api(description = "我的卡券接口")
@RestController
@RequestMapping("/member/couponCode")
public class CouponCodeMemberController extends BaseController {

    @Autowired
    private CouponCodeService couponCodeService;

    @Autowired
    private CouponService couponService;

    @Autowired
    private MemberService memberService;

    @Autowired
    private SysSequenceService sysSequenceService;

    /**
     * 分页查询我的卡券
     *
     * @return
    */
    @ApiOperation("分页查询我的卡券")
    @GetMapping("/list")
    @ApiImplicitParams({
    })
    public CommResult<CouponCode> list(Pageable pageable) {
        Map<String, Object> params = new HashMap<String, Object>();
        params = buildSortField(params, pageable);

        Member member =  memberService.getCurrent();

        params.put("memberId",member.getId());

        Page<Object> startPage = PageHelper.startPage(pageable.getPageNum(), pageable.getPageSize());
        List<CouponCodeVo> list = couponCodeService.selectVoList(params);
        PageResult<CouponCodeVo> pageResult = new PageResult<CouponCodeVo>(list, startPage.getTotal(), pageable);
        return CommResult.success(pageResult);
    }


    /**
     * 获取优惠券数量
     *
     * @return
     */
    @ApiOperation("获取优惠券数量")
    @GetMapping("/count")
    public CommResult<Long> count() {
        Member member = memberService.getCurrent();
        Map<String,Object> params = new HashMap<>();
        params.put("memberId",member.getId());
        params.put("deleted",false);
        params.put("isUsed",false);
        params.put("vaildDate",new Date());
        BigDecimal count = couponCodeService.selectSumCount(params);
        return CommResult.success(
                count.setScale(0,BigDecimal.ROUND_HALF_DOWN).longValue()
        );

    }


    /**
     * 查询单条我的卡券
     *
     * @return
    */
    @ApiOperation("查询单条我的卡券")
    @GetMapping("/find/{id}")
    public CommResult<CouponCode> find(@PathVariable String id) {
        CouponCode result = couponCodeService.findById(id);

        Map<String,Object> data = new HashMap<>();
        data.put("couponCode",result);
        return CommResult.success(data);

    }

    /**
     * 领券接口
     *
     * @param couponId
     * @return
     */
    @ApiOperation("领券接口")
    @PostMapping("/create")
    public CommResult<CouponCode> create(Long couponId) {

        Coupon coupon = couponService.findById(couponId);
        if (coupon==null) {
            return CommResult.error("无效优惠券");
        }

        if (coupon.getDeleted()) {
            return CommResult.error("已结束了");
        }

        if (coupon.getStock()<=0) {
            return CommResult.error("已领完了");
        }

        if (coupon.getMaxgiven()==null) {
            coupon.setMaxgiven(0L);
        }

        Member member = memberService.getCurrent();

        if (coupon.getMaxgiven()>0) {
            Map<String,Object> params = new HashMap<>();
            params.put("couponId",coupon.getId());
            params.put("memberId",member.getId());
            Integer w = couponCodeService.selectRowCount(params);
            if (w>=coupon.getMaxgiven()) {
                return CommResult.error("不能再领了");
            }
        }

        String sn = sysSequenceService.generate("couponCode");

        couponCodeService.create(coupon,member.getId(),sn,1);

        return CommResult.success("领取成功");

    }

    /**
     * 批量删除我的卡券
     *
     * @param ids
     * @return
     */
    @Log(desc = "批量删除我的卡券", type = Log.LOG_TYPE.DEL)
    @ApiOperation("批量删除我的卡券,ids用逗号拼接")
    @PostMapping("/del/{ids}")
    public CommResult<CouponCode> del(@PathVariable String ids) {

        couponCodeService.deleteByIds(ids.split(","));
        return CommResult.success();
    }


}
